package com.jwm.power.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.zhxu.bs.MapSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.util.MapUtils;
import com.jwm.power.model.entity.TaskLst;
import com.jwm.power.model.entity.Users;
import com.jwm.power.model.reponse.ResponseData;
import com.jwm.power.service.TaskService;
import com.jwm.power.service.UsersService;
import com.jwm.power.util.Ut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/tasklst")
public class TaskSendController {
    @Autowired
    private MapSearcher mapSearcher;
    @Resource
    private TaskService taskService;
    @RequestMapping(value = "/queryTasklst", method = RequestMethod.GET)
    public void QueryTaskLst(HttpServletRequest request, HttpServletResponse response){
        try{
            String page = request.getParameter("cPage");
            String size = request.getParameter("pSize");
            String searchName = request.getParameter("searchName");
            String queryBeginSeachDate =request.getParameter("queryBeginSeachDate");
            String queryEndSearchDate =request.getParameter("queryEndSearchDate");
            Users users = (Users) StpUtil.getSession().get("users");
            Map<String, Object> params = MapUtils.builder()
                    .group("A")         // 分组加载，A不作判定条件，当请求条件满足B或C时加载，不满足不加载数据
                    .page(Integer.parseInt(page) - 1 , Integer.parseInt(size))                   // 偏移 0 条，查询 15 条（推荐写法）
                    .field("taskcontent", searchName).op("ct")
                    .field("taskfirstdate", queryBeginSeachDate,queryEndSearchDate).op("bt")
                    .group("B")
                    .field("sendtaskuser", users.getName())
                    .group("C")
                    .field("maketaskuser", users.getName())
                    .groupExpr("A&(C|B)")
                    .build();
            Map<String, Object> dataMap = new HashMap<>();
            SearchResult<Map<String, Object>> searchResult = mapSearcher.search(TaskLst.class, params);
            dataMap.put("data", searchResult.getDataList());
            dataMap.put("totals", searchResult.getTotalCount());
            Ut.toJson(response, dataMap);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
    // 更新表单数据为已读
    @RequestMapping(value = "/taskIsRead", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData TaskIsRead(HttpServletRequest request, HttpServletResponse response) throws ParseException{
        return taskService.TaskIsRead(request);
    }
    // 更新逾期表单
    @RequestMapping(value = "/UpdateOverfeedback", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData UpdateOverfeedback(HttpServletRequest request, HttpServletResponse response) throws ParseException{
        return taskService.UpdateOverfeedback(request);
    }
    // 更新完成表单
    @RequestMapping(value = "/UpdateComplate", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData UpdateComplate(HttpServletRequest request, HttpServletResponse response) throws ParseException{
        return taskService.UpdateComplate(request);
    }
    // 获取当前任务列表
    @RequestMapping(value = "/saveTasklst", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData SaveTasklst(HttpServletRequest request, HttpServletResponse response) throws ParseException{
        return taskService.SaveTasklst(request);
    }
}
